package ControlLayer;
import ModelLayer.*;
import java.util.ArrayList;


/**
 * This is the controller of the class Person, it controls which methods that should be sent to the UI layer.
 * 
 * @author Team Black
 * @version 1.0
 */
public class PersonCtr
{   
    private CustomerLibrary customerLibrary;
    private EmployeeLibrary employeeLibrary;
    
    public PersonCtr()
    {
        customerLibrary = CustomerLibrary.getInstance();
        employeeLibrary = employeeLibrary.getInstance();
    }
    /**
     * Creates a costumer.
     */
    public void createCustomer(String _name, String _adress, String _zipCode, String _phoneNr, int _discount) 
    {
        Customer _customer = new Customer(_name, _adress, _zipCode, _phoneNr);
        _customer.setDiscount(_discount);
        customerLibrary.addCustomer(_customer);
    }
    /**
     * adds an object of costumer to the costumer list.
     */
    public void addCustomer(Customer _customer)
    {
        customerLibrary.addCustomer(_customer);
    }
    /**
     * search for Costumer by the ID
     */
    public Customer searchCustomerById(int _id)
    {
        return customerLibrary.searchById(_id);
    }
    /**
     * search for Costumer by the name
     */
    public Customer searchCustomerByName(String _name)
    {
        return customerLibrary.searchByName(_name);
    }
    /**
     * updates the Costumer, first search by ID, then change all the fields of Costumer; name, address, zipcode, phoneNr. and discount
     */
    public void updateCustomer(int _id, String _name, String _adress, String _zipCode, String _phoneNr, long _discount)
    {
        Customer customer = searchCustomerById(_id);
        customer.setName(_name);
        customer.setAdress(_adress);
        customer.setZipCode(_zipCode);
        customer.setPhoneNr(_phoneNr);
        customer.setDiscount(_discount);
    }
    /**
     * deletes an object of Costumer from the list, by ID.
     */
    public void deleteCustomer(int _id)
    {
        Customer customer = searchCustomerById(_id);
        customerLibrary.removeCustomer(customer);
    }
    /**
     * deletes an object of Costumer from the list, by name.
     */
    public void deleteCustomer(String _name)
    {
        Customer customer = searchCustomerByName(_name);
        customerLibrary.removeCustomer(customer);
    }
    /**
     * creates a employee
     */
    public void createEmployee(String _name, String _adress, String _zipCode, String _phoneNr, String _salaryAccount, int _discount) //HVORFOR IKKE OGSÅ RABAT?
    {
        Employee employee = new Employee(_name, _adress, _zipCode, _phoneNr, _salaryAccount);
        employee.setDiscount(_discount);
        addEmployee(employee);
    }
    /**
     * adds an object of employee to the employee list.
     */
    public void addEmployee(Employee _employee)
    {
        employeeLibrary.addEmployee(_employee);
    }
    /**
     * search for employee by the ID
     */
    public Employee searchEmployeeById(int _id)
    {
        return employeeLibrary.searchById(_id);
    }
    /**
     * search for employee by the Name
     */
    public Employee searchEmployeeByName(String _name)
    {
        return employeeLibrary.searchByName(_name);
    }
    /**
     * updates the employee, first search by ID, then change all the fields of employee; name, address, zipcode, salaryaccount, phoneNr. and discount
     */
    public void updateEmployee(int _id, String _name, String _adress, String _zipCode, String _phoneNr, String _salaryAccount, long _discount)
    {
        Employee employee = searchEmployeeById(_id);
        employee.setName(_name);
        employee.setAdress(_adress);
        employee.setZipCode(_zipCode);
        employee.setPhoneNr(_phoneNr);
        employee.setSalaryAccount(_salaryAccount);
        employee.setDiscount(_discount);
    }
    /**
     * deletes an object of employee from the list, by ID.
     */
    public void deleteEmployee(int _id)
    {
        Employee employee = searchEmployeeById(_id);
        employeeLibrary.removeEmployee(employee);
    }
    /**
     * deletes an object of employee from the list, by name.
     */
    public void deleteEmployee(String _name)
    {
        Employee employee = searchEmployeeByName(_name);
        employeeLibrary.removeEmployee(employee); 
    }
    /**
     * returns a list of all costumers
     */
    public String returnAllCustomers()
    {
        String retStr ="";
        
        ArrayList<Customer> allCustomer = customerLibrary.getInstance().returnAllCustomers();
        
        if(allCustomer != null)
        {
            retStr += " Alle kunder:\n\n";
            
            for(Customer newCustomer: allCustomer)
            {
                retStr += "id: " + newCustomer.getId() + " navn: " + newCustomer.getName()+"\n";
            }
        }
        return retStr;
    }
    /**
     * returns a list of all employees
     */
    public String returnAllEmployee()
    {
        String retStr ="";
        
        ArrayList<Employee> allEmployee = employeeLibrary.getInstance().returnAllEmployee();
        
        if(allEmployee != null)
        {
            retStr += " Alle ekspedienter:\n\n";
            
            for(Employee newEmployee: allEmployee)
            {
                retStr += "id: " + newEmployee.getId() + " navn: " + newEmployee.getName()+"\n";
            }
        }
        return retStr;
    }
}